package com.example.modellol.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.modellol.R;
import com.example.modellol.bean.Gent;

import java.util.ArrayList;

public class MyAdapter4 extends RecyclerView.Adapter<MyAdapter4.myViewHodler> {
    private Context context;
    private ArrayList<Gent> goodsEntityList;

    //创建构造函数
    public MyAdapter4(Context context, ArrayList<Gent> goodsEntityList) {
        //将传递过来的数据，赋值给本地变量
        this.context = context;//上下文
        this.goodsEntityList = goodsEntityList;//实体类数据ArrayList
    }

    @Override
    public myViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建自定义布局
        View itemView = View.inflate(context, R.layout.fragment4, null);
        return new myViewHodler(itemView);
    }

    @Override
    public void onBindViewHolder(myViewHodler holder, int position) {
        //根据点击位置绑定数据
        Gent data = goodsEntityList.get(position);
//        holder.mItemGoodsImg;
        holder.text_1.setText(data.getText_1());
        holder.text_2.setText(data.getText_2());
        holder.photo_1.setImageResource(data.getPhoto_1());
        holder.photo_2.setImageResource(data.getPhoto_2());


    }

    @Override
    public int getItemCount() {
        return goodsEntityList.size();
    }

    //自定义viewhodler
    class myViewHodler extends RecyclerView.ViewHolder {
        private ImageView photo_1;
        private ImageView photo_2;
        private TextView text_1;
        private TextView text_2;


        public myViewHodler(View itemView) {
            super(itemView);
            photo_1 = (ImageView) itemView.findViewById(R.id.gent_photo);
            photo_2 = (ImageView) itemView.findViewById(R.id.gent_io);
            text_1 = (TextView) itemView.findViewById(R.id.gent_line_name);
            text_2 = (TextView) itemView.findViewById(R.id.gent_Anchor);
            //点击事件放在adapter中使用，也可以写个接口在activity中调用
            //方法一：在adapter中设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //可以选择直接在本位置直接写业务处理
                    //Toast.makeText(context,"点击了xxx",Toast.LENGTH_SHORT).show();
                    //此处回传点击监听事件
                    if(onItemClickListener!=null){
                        onItemClickListener.OnItemClick(v, goodsEntityList.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        public void OnItemClick(View view, Gent data);
    }

    //需要外部访问，所以需要设置set方法，方便调用
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
